﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

namespace Hired
{
    public partial class empprofile : System.Web.UI.Page
    {
        private CompanyAccount ca;
        private EmployerPassport ep;

        protected void Page_Init(object sender, EventArgs e)
        {
            if (User.IsInRole("company"))
            {
                ca = DBCompanyAccount.RetrieveCompanyAccount(User.Identity.Name, true);
                lblUsername.Text = ca.Username;
            }
            else if (User.IsInRole("employer"))
            {
                ep = DBEmployerPassport.RetrieveEmployerPassport(User.Identity.Name);
                ca = DBCompanyAccount.RetrieveCompanyAccountByEmployerID(User.Identity.Name, true);
                lblUsername.Text = ep.Username;
            }

            else
            {
                Response.Redirect("home.aspx");
            }

            //Wall Thread
            List<Thread> wallThread = DBThread.RetrieveThreadsByUserID(ca.CAccountID);

            if (wallThread.Count != 0)
            {
                foreach (Thread t in wallThread)
                {
                    #region Thread
                    UCComments tc = (UCComments)LoadControl("UCComments.ascx");
                    pnlContainer.Controls.Add(tc);

                    // accessing ThreadComment.ascx controls
                    Image threadImgProfilePic = (Image)tc.FindControl("imgProfilePic");
                    Label threadLblCommentContent = (Label)tc.FindControl("lblCommentContent");
                    LinkButton threadBtnUsername = (LinkButton)tc.FindControl("btnUsername");

                    // assigning values to them
                    if (DBJobseekerAccount.retrieveJobseekerAccount(t.PosterID, true) != null)
                    {
                        JobseekerAccount jsPoster = DBJobseekerAccount.retrieveJobseekerAccount(t.PosterID, true);
                        threadImgProfilePic.ImageUrl = jsPoster.Jobseeker.ImageAddress;
                        threadBtnUsername.Text = jsPoster.Username;
                        threadBtnUsername.PostBackUrl = "viewprofile.aspx?username=" + jsPoster.Username;
                    }
                    else if (DBCompanyAccount.RetrieveCompanyAccount(t.PosterID, true) != null)
                    {
                        CompanyAccount coPoster = DBCompanyAccount.RetrieveCompanyAccount(t.PosterID, true);
                        threadImgProfilePic.ImageUrl = coPoster.Company.ImageAddress;
                        threadBtnUsername.Text = coPoster.Username;
                        threadBtnUsername.PostBackUrl = "viewprofile.aspx?username=" + coPoster.Username;
                    }
                    threadLblCommentContent.Text = t.Contents;

                    // assigning values to them
                    foreach (Comment c in t.Comments)
                    {
                        UCComments comment = (UCComments)Page.LoadControl("UCComments.ascx");
                        pnlContainer.Controls.Add(comment);

                        // accessing ThreadUC.ascx controls
                        Panel commentPnlComment = (Panel)comment.FindControl("pnlComment");
                        Image commentImgProfilePic = (Image)comment.FindControl("imgProfilePic");
                        Label commentLblCommentContent = (Label)comment.FindControl("lblCommentContent");
                        LinkButton commentBtnUsername = (LinkButton)comment.FindControl("btnUsername");

                        // assigning values to them
                        commentPnlComment.CssClass = "pnlComment";
                        if (DBJobseekerAccount.retrieveJobseekerAccount(t.PosterID, true) != null)
                        {
                            JobseekerAccount jsPoster = DBJobseekerAccount.retrieveJobseekerAccount(t.PosterID, true);
                            commentImgProfilePic.ImageUrl = jsPoster.Jobseeker.ImageAddress;
                            commentBtnUsername.Text = jsPoster.Username;
                            commentBtnUsername.PostBackUrl = "viewprofile.aspx?username=" + jsPoster.Username;
                        }
                        else if (DBCompanyAccount.RetrieveCompanyAccount(t.PosterID, true) != null)
                        {
                            CompanyAccount coPoster = DBCompanyAccount.RetrieveCompanyAccount(t.PosterID, true);
                            commentImgProfilePic.ImageUrl = coPoster.Company.ImageAddress;
                            commentBtnUsername.Text = coPoster.Username;
                            commentBtnUsername.PostBackUrl = "viewprofile.aspx?username=" + coPoster.Username;
                        }
                        commentLblCommentContent.Text = t.Contents;
                    }
                    threadLblCommentContent.Text = t.Contents;
                    #endregion

                    #region Comments
                    if (t.Comments.Count != 0)
                    {
                        foreach (Comment c in t.Comments)
                        {
                            UCComments comment = (UCComments)Page.LoadControl("UCComments.ascx");
                            pnlContainer.Controls.Add(comment);

                            // accessing ThreadUC.ascx controls
                            Panel commentPnlComment = (Panel)comment.FindControl("pnlComment");
                            Image commentImgProfilePic = (Image)comment.FindControl("imgProfilePic");
                            Label commentLblCommentContent = (Label)comment.FindControl("lblCommentContent");
                            LinkButton commentBtnUsername = (LinkButton)comment.FindControl("btnUsername");

                            // assigning values to them
                            commentPnlComment.CssClass = "pnlComment";
                            if (DBJobseekerAccount.retrieveJobseekerAccount(t.PosterID, true) != null)
                            {
                                JobseekerAccount jsPoster = DBJobseekerAccount.retrieveJobseekerAccount(t.PosterID, true);
                                commentImgProfilePic.ImageUrl = jsPoster.Jobseeker.ImageAddress;
                                commentBtnUsername.Text = jsPoster.Username;
                                commentBtnUsername.PostBackUrl = "viewprofile.aspx?username=" + jsPoster.Username;
                            }
                            if (DBCompanyAccount.RetrieveCompanyAccount(t.PosterID, true) != null)
                            {
                                CompanyAccount coPoster = DBCompanyAccount.RetrieveCompanyAccount(t.PosterID, true);
                                commentImgProfilePic.ImageUrl = coPoster.Company.ImageAddress;
                                commentBtnUsername.Text = coPoster.Username;
                                commentBtnUsername.PostBackUrl = "viewprofile.aspx?username=" + coPoster.Username;
                            }
                            commentLblCommentContent.Text = c.Contents;
                        }
                    }
                    #endregion

                    UCPostComment pc = (UCPostComment)Page.LoadControl("UCPostComment.ascx");
                    ((Label)pc.FindControl("lblThreadID")).Text = t.ThreadID;
                    pnlContainer.Controls.Add(pc);

                    Button btnPost = (Button)pc.FindControl("btnPost");
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void linkBtnViewAll_Click(object sender, EventArgs e)
        {
            Response.Redirect("empViewJob.aspx?company=" + Server.UrlEncode(ca.CAccountID));
        }

        protected void btnWall_Click(object sender, ImageClickEventArgs e)
        {
            hidePanels();
            pnlWall.Visible = true;
        }

        protected void btnComment_Click(object sender, EventArgs e)
        {
            Thread t = new Thread();
            t.Contents = txtComment.Text;
            t.PosterID = User.Identity.Name;
            if (User.IsInRole("company"))
            {
                t.WallID = ca.CAccountID;
            }
            else if (User.IsInRole("employer"))
            {
                ca = DBCompanyAccount.RetrieveCompanyAccountByEmployerID(ep.EPassportID, false);
                t.WallID = ca.CAccountID;
            }
            DBThread.AddThread(t);
        }

        protected void btnInformation_Click(object sender, ImageClickEventArgs e)
        {

            hidePanels();
            pnlPersonalInformation.Visible = true;
            #region
            //CompanyInfo
            lblCompanyNameValue.Text = ca.Company.CompanyName;
            lblDateFoundedValue.Text = ca.Company.DateFounded.ToString();
            lblContactNumberValue.Text = ca.Company.ContactNumber;
            lblWebsiteValue.Text = ca.Company.WebsiteURL;
            lblEmailAddressValue.Text = ca.Email;
            lblAddressValue.Text = ca.Company.Address;
            lblPostalCodeValue.Text = ca.Company.PostalCode;

            //EmployerInfo
            if (ep != null)
            {
                lblFirstNameValue.Text = ep.FirstName;
                lblLastNameValue.Text = ep.LastName;
                pnlEmployerInformation.Visible = true;
            }
            #endregion
        }

        protected void lblUpdateInfo_Click(object sender, EventArgs e)
        {
            hidePanels();
            pnlUpdateInformation.Visible = true;

            #region
            //CompanyInfo
            txtCompanyName.Text = ca.Company.CompanyName;
            txtDateFounded.Text = ca.Company.DateFounded.ToString();
            txtContactNum.Text = ca.Company.ContactNumber;
            txtWebsiteUrl.Text = ca.Company.WebsiteURL;
            lblEmailValue.Text = ca.Email;
            txtAddress1.Text = ca.Company.Address;
            txtPostalCode.Text = ca.Company.PostalCode;

            //EmployerInfo
            if (ep != null)
            {
                txtFirstName.Text = ep.FirstName;
                txtLastName.Text = ep.LastName;
                pnlEmployerInformation.Enabled = true;
                pnlEmployerInformation.Visible = true;

            }

            #endregion
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                #region
                //CompanyInfo
                ca.Company.CompanyName = txtCompanyName.Text;
                ca.Company.DateFounded = DateTime.Parse(txtDateFounded.Text);
                ca.Company.ContactNumber = txtContactNum.Text;
                ca.Company.WebsiteURL = txtWebsiteUrl.Text;
                ca.Company.Address = txtAddress1.Text;
                ca.Company.PostalCode = txtPostalCode.Text;

                //EmployerInfo
                if (ep != null)
                {
                    ep.FirstName = txtFirstName.Text;
                    ep.LastName = txtLastName.Text;
                    DBEmployerPassport.UpdateEmployerPassport(ep);
                }
                #endregion

                DBCompanyAccount.UpdateCompanyAccount(ca);
                DBCompany.UpdateCompany(ca.Company);
                hidePanels();
                pnlWall.Visible = true;
            }
        }

        //Validate
        protected void CustomDateFounded_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string stringDateFounded = txtDateFounded.Text;
            CultureInfo ci = new CultureInfo("es-ES");
            DateTime inputDOB = DateTime.Parse(stringDateFounded, ci);

            if (inputDOB < DateTime.Now)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        protected void btnBuddyList_Click(object sender, ImageClickEventArgs e)
        {
            hidePanels();
            pnlBuddyList.Visible = true;

            lvBuddyList.DataSource = DBBuddyRoster.viewBuddyImageNameList(User.Identity.Name);
            lvBuddyList.DataBind();
        }

        protected void btnInbox_Click(object sender, ImageClickEventArgs e)
        {
            hidePanels();
            pnlInbox.Visible = true;

            gvInbox.DataSource = DBMessage.retrieveInbox(User.Identity.Name);
            gvInbox.DataBind();
        }

        protected void btnImages_Click(object sender, ImageClickEventArgs e)
        {
            hidePanels();
            pnlImages.Visible = true;
            
        }

        //hidePanels()
        protected void hidePanels()
        {
            pnlWall.Visible = false;
            pnlPersonalInformation.Visible = false;
            pnlBuddyList.Visible = false;
            pnlImages.Visible = false;
            pnlUpdateInformation.Visible = false;
            pnlInbox.Visible = false;
            pnlBuddyRequests.Visible = false;
        }

        protected void gvInbox_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Select":
                    pnlMessage.Visible = true;

                    try
                    {
                        Message m = DBMessage.RetrieveMessage((string)e.CommandArgument);

                        switch (DBAccount.RetrieveAccountTypeByID(m.SenderID))
                        {
                            case "CompanyAccount":
                                CompanyAccount ca = DBCompanyAccount.RetrieveCompanyAccount(m.SenderID, false);
                                lblMessageSender.Text = ca.Username;
                                break;
                            case "JobseekerAccount":
                                JobseekerAccount jsa = DBJobseekerAccount.retrieveJobseekerAccount(m.SenderID, false);
                                lblMessageSender.Text = jsa.Username;
                                break;
                        }
                        lblMessageTitle.Text = m.Title;
                        lblMessageContents.Text = m.Contents;
                    }
                    catch (Exception)
                    {
                        lblMessageSender.Text = "N/A";
                        lblMessageTitle.Text = "N/A";
                        lblMessageContents.Text = "Message does not exist!";
                    }
                    break;
                case "Delete":
                    DBMessage.DeleteMessagebyId((string)e.CommandArgument);
                    break;
            }
        }

        protected void lblBuddyRequest_Click(object sender, EventArgs e)
        {
            hidePanels();
            pnlBuddyRequests.Visible = true;

            gvBuddyRequests.DataSource = DBBuddyRequestRoster.retrieveBuddyRequestListForDisplay(User.Identity.Name);
            gvBuddyRequests.DataBind();
        }

        protected void gvBuddyRequests_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Accept":
                    BuddyRoster br = new BuddyRoster();
                    br.BuddyID1 = (string)e.CommandArgument;
                    br.BuddyID2 = User.Identity.Name;

                    DBBuddyRequestRoster.deleteBuddyRequest((string)e.CommandArgument, User.Identity.Name);
                    DBBuddyRoster.addBuddy(br);

                    gvBuddyRequests.DataSource = DBBuddyRequestRoster.retrieveBuddyRequestListForDisplay(User.Identity.Name);
                    gvBuddyRequests.DataBind();
                    break;
                case "Decline":
                    DBBuddyRequestRoster.deleteBuddyRequest((string)e.CommandArgument, User.Identity.Name);
                    break;
            }
        }
    }
}